function gamemap(mapFile){
    this.mapFile = mapFile;
    this.mapName;
    this.mapPlayers;
    this.tileHeight = 16;
    this.tileWidth = 32;
    this.height;
    this.width;
    this.zoom = 3;
    this.timePlayed;
    this.mapBG = {};
    this.cursor = {};
    this.grid = {};
    this.resCount = 0;
    this.res = {};
    this.structures = {};
    this.entities = {};
    this.noids = {};
    this.tasks = {};

    this.pathGrid = [];
}
gamemap.prototype.load = function(){
    
    /**
     * Background
     */
    $('#pageContainer').css("width", window.innerWidth).css("height",window.innerHeight);
    $(document).on("scroll",function(){
        $('body').scrollTop(0).scrollLeft(0);
    })
    $(document).attr('unselectable', 'on').css('user-select', 'none').on('selectstart', false);

    this.mapBG.colorBack = new createjs.Shape();
    this.mapBG.colorBack.graphics.beginFill('#000').drawRect(0, 0, window.innerWidth, window.innerHeight);
    game.stage.addChild(this.mapBG.colorBack);
    //Stars
    this.mapBG.stars = {};
    this.mapBG.stars.group = new createjs.Container();
    this.mapBG.stars.group.regX = (window.innerWidth/2)>>0;
    this.mapBG.stars.group.regY = (window.innerHeight/2)>>0;
    this.mapBG.stars.group.x = this.mapBG.stars.group.regX;
    this.mapBG.stars.group.y = this.mapBG.stars.group.regY;
    this.mapBG.stars.group.width = (window.innerWidth)>>0;
    this.mapBG.stars.group.height = (window.innerHeight)>>0;
    for(var i=0; i<300; i++){
        var x = ((Math.random()-0.5)*window.innerWidth)>>0;
        var y = ((Math.random()-0.5)*window.innerWidth)>>0;
        x += this.mapBG.stars.group.regX;
        y += this.mapBG.stars.group.regY;
        var angle = (Math.random()*90)>>0;
        this.mapBG.stars['s'+i] = new createjs.Shape();
        this.mapBG.stars['s'+i].graphics.beginFill('#fff').drawPolyStar(x, y, 2, 4, 1.8, angle);
        this.mapBG.stars.group.addChild(this.mapBG.stars['s'+i]);
    }

    game.stage.addChild(this.mapBG.stars.group);
    
    this.mapBG.stars2 = {};
    this.mapBG.stars2.group = new createjs.Container();
    this.mapBG.stars2.group.regX = (window.innerWidth/2)>>0;
    this.mapBG.stars2.group.regY = (window.innerHeight/2)>>0;
    this.mapBG.stars2.group.x = this.mapBG.stars.group.regX;
    this.mapBG.stars2.group.y = this.mapBG.stars.group.regY;
    this.mapBG.stars2.group.width = (window.innerWidth)>>0;
    this.mapBG.stars2.group.height = (window.innerHeight)>>0;
    for(var i=0; i<300; i++){
        var x = ((Math.random()-0.5)*window.innerWidth)>>0;
        var y = ((Math.random()-0.5)*window.innerWidth)>>0;
        x += this.mapBG.stars2.group.regX;
        y += this.mapBG.stars2.group.regY;
        var angle = (Math.random()*90)>>0;
        this.mapBG.stars2['s'+i] = new createjs.Shape();
        this.mapBG.stars2['s'+i].graphics.beginFill('#fff').drawPolyStar(x, y, 1, 2, 0.6, angle);
        this.mapBG.stars2.group.addChild(this.mapBG.stars2['s'+i]);
    }

    game.stage.addChild(this.mapBG.stars2.group);

    //Main container
    game.mainContainer = new createjs.Container();
    game.stage.addChild(game.mainContainer);
    game.mainContainer.x = game.mainContainer.y = 0;

    /**
     * Structures & grid
     */
    $.ajax({
        url: "./data/maps/"+this.mapFile+".json",
        dataType:"json"})
        .done(function(data){ game.gameMap.loadJson(data); });
}
gamemap.prototype.loadJson = function(data){
    /**
     * Global
     */
    game.gameMap.mapName = data.name;
    game.gameMap.mapPlayers = data.players;
    game.gameMap.width = data.width;
    game.gameMap.height = data.height;
    game.mainContainer.width = data.width * game.gameMap.tileWidth;
    game.mainContainer.height = data.height * game.gameMap.tileHeight;
    $('#gameBackground').width(data.width * game.gameMap.tileWidth);
    $('#gameBackground').height(data.height * game.gameMap.tileHeight);

    /**
     * grid
     */
    var pair = true;
    game.gameMap.grid.cases = [];
    game.gameMap.grid.pathGrid = [];
    var map = document.createElement('map');
    map.setAttribute('name','gamemap');
    map.setAttribute('id','gamemap');
    document.getElementById('pageContainer').appendChild(map);

    for(var row = 0; row < (data.height*2); row++){
        game.gameMap.grid.cases[row] = [];
        for(var col = 0; col < data.width; col++){
            var posx = col * game.gameMap.tileWidth;
            var posy = row * (game.gameMap.tileHeight/2);
            if(!pair)
                posx += (game.gameMap.tileWidth/2);

            var shapeCoords = posx+","+(posy-8)+","+(posx-16)+","+posy+","+posx+","+(posy+8)+","+(posx+16)+","+posy;

            var AStarCoords = game.gameMap.convertGridCoordsToAStarCoords([col,row]);
            game.gameMap.grid.cases[row][col] = document.createElement('area');
            game.gameMap.grid.cases[row][col].setAttribute('shape','poly');
            game.gameMap.grid.cases[row][col].setAttribute('coords', shapeCoords);
            game.gameMap.grid.cases[row][col].setAttribute('data-cell',"#cell"+col+"_"+row);
            game.gameMap.grid.cases[row][col].setAttribute('data-astar',"#cell"+AStarCoords.join('_'));
            game.gameMap.grid.cases[row][col].setAttribute('id',col+'-'+row);
            game.gameMap.grid.cases[row][col].setAttribute('data-posx',posx);
            game.gameMap.grid.cases[row][col].setAttribute('data-posy',posy);
            game.gameMap.grid.cases[row][col].onclick = function(evt){
                evt.preventDefault();
            };
            game.gameMap.grid.cases[row][col].onmouseover = function(evt){
                game.gameGui.moveCursor(evt);
            };
            game.gameMap.grid.cases[row][col].onmouseup = function(evt){
                game.gameGui.clickCursor(evt);
            };
            game.gameMap.grid.cases[row][col].onmousedown = function(evt){
                game.gameGui.startDragCursor(evt);
            };
            game.gameMap.grid.cases[row][col].oncontextmenu = function(evt){
                game.gameGui.cancelBuild(evt);
            };
            map.appendChild(game.gameMap.grid.cases[row][col]);
        }
        pair = !pair;
    }
    for(var x = 0; x < (data.width*2); x++){
        game.gameMap.grid.pathGrid[x] = [];
        for(var y = 0; y < (data.height*2); y++)
                game.gameMap.grid.pathGrid[x][y] = 0;
    }
    document.getElementById('gameBackground').setAttribute('usemap',"#gamemap");
    $('#gamemap').bind('mousewheel DOMMouseScroll', function(evt){
        evt.preventDefault();
        if (evt.originalEvent.wheelDelta > 0 || evt.originalEvent.detail < 0) {
            //zoom out
            if(game.gameMap.zoom > 2){
                game.gameMap.zoom--;
            }
        }else{
            //zoom in
            if(game.gameMap.zoom < 4){
                game.gameMap.zoom++;
            }
        }
        switch(game.gameMap.zoom){
            case 1:
                game.mainContainer.scaleX = 0.25;
                game.mainContainer.scaleY = 0.25;
                break;

            case 2:
                game.mainContainer.scaleX = 0.5;
                game.mainContainer.scaleY = 0.5;
                break;

            case 3:
                game.mainContainer.scaleX = 1;
                game.mainContainer.scaleY = 1;
                break;

            case 4:
                game.mainContainer.scaleX = 2;
                game.mainContainer.scaleY = 2;
                break;
        }
        $('#gameBackground').attr('class', "zoom"+game.gameMap.zoom);
    });
    $(document).bind('keyup', function(evt){
        var key = evt.keyCode;
        if(game.gameGui.constructMode == true && key == 32){
            evt.preventDefault();
            game.gameGui.rotateConstruct(evt);
        }
    });
    $('#guiTop, #guiBottom').bind('mouseover',function(evt){
        $('#cursor').attr('class',"pointer");
        $('#gamemap').off("mousemove");
    }).bind('contextmenu',function(evt){
        evt.preventDefault();
    });

    /**
     * Structures
     */
    //spawn player
    for(var i in data.structures.spawns){
        data.structures.spawns[i] = game.gameMap.convertFileCoordsToGridCoords(data.structures.spawns[i]);
        game.gameMap.structures["player"+i] = {};
        game.gameMap.structures["player"+i]["spawn"] = new gamestructure("sp"+i, "spawn", null, null, null, -1);
        game.gameMap.structures["player"+i]["spawn"].place(data.structures.spawns[i].realx, data.structures.spawns[i].realy, null, {"player":null});

        /**
         * Noids
         */
        for(var j=0; j < data.noids; j++){
            var id = game.gameMap.noids.length;
            game.gameMap.noids[id] = new gamenoid(id, 3, (j+1), null);
            game.gameMap.noids[id].spawn(data.structures.spawns[i].x/2, data.structures.spawns[i].y);
        }

        /**
         * Resources
         */
        for(var j=0; j < data.resources.thorium; j++){
            var id = game.gameMap.resCount;
            game.gameMap.structures["player"+i]["spawn"].resourcesStorage[id] = new gameentity(id,"thorium",null);
            game.gameMap.structures["player"+i]["spawn"].resourcesStorage[id].place(data.structures.spawns[i].x/2 - 0.5, data.structures.spawns[i].y - 1);
            game.gameMap.resCount++;
        }

        for(var j=0; j < data.resources.energy; j++){
            var id = game.gameMap.resCount;
            game.gameMap.structures["player"+i]["spawn"].resourcesStorage[id] = new gameentity(id,"energy",null);
            game.gameMap.structures["player"+i]["spawn"].resourcesStorage[id].place(data.structures.spawns[i].x/2 - 1.5, data.structures.spawns[i].y);
            game.gameMap.resCount++;
        }
        //
        //for(var j=0; j < data.resources.harvest; j++){
        //    var id = game.gameMap.res.length;
        //    game.gameMap.res[id] = new gameentity(id,"harvest",null);
        //    game.gameMap.res[id].place(data.structures.spawns[i].realx + game.gameMap.tileWidth, data.structures.spawns[i].realy);
        //}
        //
        //for(var j=0; j < data.resources.food; j++){
        //    var id = game.gameMap.res.length;
        //    game.gameMap.res[id] = new gameentity(id,"food",null);
        //    game.gameMap.res[id].place(data.structures.spawns[i].realx, data.structures.spawns[i].realy + game.gameMap.tileHeight);
        //}
    }

    //thorium
    for(var i in data.structures.thorium){
        data.structures.thorium[i] = game.gameMap.convertFileCoordsToGridCoords(data.structures.thorium[i]);
        game.gameMap.structures["resources"] = {};
        game.gameMap.structures["resources"]["thorium"+i] = new gamestructure("th"+i, "thorium", null, null, null, -1);
        game.gameMap.structures["resources"]["thorium"+i].place(data.structures.thorium[i].realx, data.structures.thorium[i].realy, null, {"size":data.structures.thorium[i].size});
    }
    
    game.gameRenderer.stopRendering = false;
    $('#guiTop').show();
    $('#guiBottom').show();
}
gamemap.prototype.update = function(frame){
    this.mapBG.stars.group.rotation += 0.04;
    this.mapBG.stars2.group.rotation += 0.02;
}
gamemap.prototype.convertFileCoordsToGridCoords = function(build){
    build.realx = (build.x>>0);
    build.realy = ((build.y * 2)>>0);
    return build;
}
gamemap.prototype.convertGridCoordsToAStarCoords = function(coords){
    var astarX = ((coords[1]>>0) % 2 == 0) ? (((coords[0]>>0) * 2) - 1) : ((coords[0]>>0) * 2);
    return [astarX, coords[1]>>0]
}
gamemap.prototype.convertAStarCoordsToGridCoords = function(coords){
    var Row = coords[1]>>0;
    var Col = (Row % 2 == 0) ? ((coords[0]>>0) + 1)/2 : (coords[0]>>0)/2 ;
    return [Col,Row];
}
gamemap.prototype.convertGridCoordsToPlacementCoords = function(coords){
    var posx = coords[0] * game.gameMap.tileWidth;
    var posy = coords[1] * (game.gameMap.tileHeight/2);
    (coords[1] % 2 != 0) ? posx += (game.gameMap.tileWidth/2) : false;
    return [posx,posy];
}
